#include "tmodule.h"
#include <QLabel>
#include <json/json.h>
#include <iostream>
#include <QDateTime>
TModule::TModule(QWidget* parent)
    : QMainWindow(parent)
    , m_pData(new QLabel(this))
    , m_pWeatherLabel(new QLabel(this))
    , m_pHumidity(new QLabel(this))
    , m_pUpdatetime(new QLabel(this))
{
    setWindowTitle("天气");
    resize(300,300);
    initUI();

    m_pClient = new hv::TcpClient;
    initConnect(port,host.c_str());
    sendInitInfo();
}

bool TModule::initConnect(int port, const char* host)
{
    int connfd = m_pClient->createsocket(port, host);
    if (connfd < 0) {
        return false;
    }
    m_pClient->onConnection = [this](const hv::SocketChannelPtr& channel) {
        if (channel->isConnected()) {
            std::cout << "TModule connected! connfd=" << channel->fd() << std::endl;
        }
        else {
            channel->close();
            std::cout << "TModule connected failed! connfd=" << channel->fd() << std::endl;
            return false;
        }
    };
    m_pClient->onMessage = [this](const hv::SocketChannelPtr& channel, hv::Buffer* buf) {
        std::cout << (char*)buf->data() << std::endl;
        std::string str = buf->base;
        Json::CharReaderBuilder reader;
        Json::Value parsedRoot;
        std::string errs;
        std::istringstream s(str);
        bool parsingSuccessful = Json::parseFromStream(reader, s, &parsedRoot, &errs);
        if (parsingSuccessful)
        {
               m_pData->setText(QString::fromStdString(parsedRoot["data"].asString()));
               m_pWeatherLabel->setText(QString::fromStdString(parsedRoot["weather"].asString()));
               m_pHumidity->setText(QString::fromStdString(parsedRoot["humunity"].asString()));
               m_pUpdatetime->setText(QDateTime::currentDateTime().toString("hh:mm:ss"));
        }
    };
    m_pClient->start();
    return true;
}

void TModule::close()
{
    SAFE_DELETE(m_pClient);
}

void TModule::initUI()
{
    m_pData->move(20,20);
    m_pData->resize(200, 40);
    m_pData->setText("日期");

    m_pWeatherLabel->move(20, 70);
    m_pWeatherLabel->resize(50,40);
    m_pWeatherLabel->setText("天气");

    m_pHumidity->move(20, 120);
    m_pHumidity->resize(50, 40);
    m_pHumidity->setText("湿度");

    m_pUpdatetime->move(20, 170);
    m_pUpdatetime->resize(150, 40);
    m_pUpdatetime->setText("更新时间");
}

TModule::~TModule()
{
    close();
}

int TModule::cmdToInt(cmd command)
{
    return static_cast<int>(command);
}

void TModule::sendInitInfo()
{
    //构建初始化连接的json字符串
    Json::Value root;
    root["cmd"] = cmdToInt(cmd::INITCONNECT);
    root["name"] = "WEATHERMODULE";
    Json::StreamWriterBuilder writer;
    std::string outputString = Json::writeString(writer, root);
    std::cout << outputString << std::endl;

    if (m_pClient == nullptr || !m_pClient->isConnected())
        std::cout << "Client has no connect with Server!!!!!!!!" << std::endl;
    else {
        std::cout << "初始化信息发送完成" << std::endl;
        m_pClient->send(outputString);
    }
}